package com.hss.spring;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class MyBatisTest {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            reader = Resources.getResourceAsReader("config/mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            //使用xml 就不用下面这句
//            sqlSessionFactory.getConfiguration().addMapper(IUser.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public static void main(String[] args) {
//        SqlSession session = sqlSessionFactory.openSession();
//        try {
            //使用xml
//            User user = (User) session.selectOne("selectUserByID", 2);
//使用注解
//            IUser iUser = session.getMapper(IUser.class);
//            session.commit();
//            User user = iUser.selectUserByID(2);

//            User user = iUser.getUser(1);
//
//            System.out.println(user.getName());
//
//            Post post = iUser.getPostById(1);
//            System.out.println(post.getContent());


//            User inserUser = new User();
//            inserUser.setName("aaaa");
//            inserUser.setAge(45);
//            inserUser.setSalary(8000);
//            iUser.insertUser(inserUser);
//            session.commit();

//           List<User> list = iUser.getUserList();
//            for (User item : list){
//                System.out.println(item.toString());
//            }



//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            session.close();
//        }
//        testGetGroupAndUsers();

        testGetUserOrders();
    }

    public static void testGetGroupAndUsers() {
        UserGroup userGroup = new UserGroup();
        SqlSession session = sqlSessionFactory.openSession();
        try {
            GroupMaper groupMaper = session.getMapper(GroupMaper.class);
            Group group = groupMaper.getGroup(1);
            System.out.println("Group => " + group.getGroupName());
            List<User> users = group.getUsers();
            for (User user : users) {
                System.out.println("\t:" + user.getId() + "\t"
                        + user.getName());
            }
        } finally {
            session.close();
        }
    }

    public static void testGetUserOrders() {
        ApplicationContext ctx;
        ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        IUser userMaper = (IUser) ctx.getBean("userMaper");
        List<Order>  orders = userMaper.getUserOrders(1);
        for (Order order : orders) {
            System.out.println("订单号："+order.getOrderNo() + "，订单金额：" + order.getMoney());
        }
    }
}
